package com.example.jh352160.surfingtv.category.customView;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.category.Category_main;
import com.example.jh352160.surfingtv.mypage.childPage.LoginPage;
import com.example.jh352160.surfingtv.network.item.RequestItem;
import com.example.jh352160.surfingtv.network.bean.CategorySubscriptionBean;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.SubscriptionService;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/25
 */

public class CategoryItem extends RelativeLayout {

    private ImageView categoryImage;
    private ImageView categorySubscriberState;
    private TextView categoryName;
    private TextView subscribeNumberView;
    private int subscribeNumber;
    private Context context;

    public boolean isSubscribe;
    private int categoryId;

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
        if (isSubscribe) {
            Category_main.thematicList.add(categoryId);
        }
    }

    public CategoryItem(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public CategoryItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        isSubscribe = false;
        LayoutInflater.from(context).inflate(R.layout.category_item, this);
        categoryImage = (ImageView) findViewById(R.id.categoryImage);
        categorySubscriberState = (ImageView) findViewById(R.id.categorySubscriberState);
        categoryName = (TextView) findViewById(R.id.categoryName);
        subscribeNumberView = (TextView) findViewById(R.id.subscribeNumber);

        categorySubscriberState.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                //api
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                SubscriptionService categoryService = retrofit.create(SubscriptionService.class);
                Call<RequestItem> call = categoryService.subscription(new CategorySubscriptionBean(categoryId));
                call.enqueue(new Callback<RequestItem>() {
                    @Override
                    public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                        if (response.body().getCode().equals("200")) {
                            subscribeNumber = Integer.parseInt(subscribeNumberView.getText().toString());
                            if (isSubscribe) {
                                categorySubscriberState.setImageResource(R.drawable.category_unsubscriber);
                                subscribeNumberView.setText("" + (subscribeNumber - 1));
                                Category_main.thematicList.remove((Integer) categoryId);
                            } else {
                                categorySubscriberState.setImageResource(R.drawable.category_subscriber);
                                subscribeNumberView.setText("" + (subscribeNumber + 1));
                                Category_main.thematicList.add(categoryId);
                            }
                            isSubscribe = !isSubscribe;
                            EventBus.getDefault().post("OK");
                        } else if (response.body().getCode().equals("4000")) {
                            Intent intent = new Intent(context, LoginPage.class);
                            context.startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailure(Call<RequestItem> call, Throwable t) {
                        Log.e("Retrofit Error", t.toString());
                    }
                });
            }
        });
    }

    public void changeStat(boolean isSubscribe) {
        this.isSubscribe = isSubscribe;
        if (isSubscribe) {
            categorySubscriberState.setImageResource(R.drawable.category_subscriber);
        }
    }
}
